# coding=utf-8
import json

"""
缓存数据对象
"""


class CacheData:
    def __init__(self, sn=None, sampleCode=None, jobnumber=None):
        # sn 码
        self.sn = sn
        # 样品码
        self.sampleCode = sampleCode
        # 工号
        self.jobnumber = jobnumber
        # 键盘值模板
        self.keyboards = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
                          27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
                          50, 51, 52, 53, 54, 56, 57, 58, 66, 67, 68, 86, 87, 88, 97, 100, 102, 103, 104, 105, 106, 107,
                          108, 109, 111, 125, 125, 183, 184, 185, 186, 187, 188, 189, 193, 194]
        # 监测到的键盘值
        self.detectKeyboardKeys = []

        # 监测键盘输入回车 28键值
        self.detectEnter = 28
        # 监测键盘输入空格 57键值
        self.detectSpace = 57

        # 鼠标值模板
        self.mouse = [1, 6, 8, 272, 273]
        # 监测鼠标值
        self.detectMouseKeys = []

        # put电池状态
        self.batteryState = "Stateless"

    def init(self):
        # sn 码
        self.sn = None
        # 样品码
        self.sampleCode = None
        # 监测到的键盘值
        self.detectKeyboardKeys = []
        # 监测鼠标值
        self.detectMouseKeys = []
        # put电池状态
        self.batteryState = "Stateless"

    @property
    def getSN(self):
        return self.sn

    def setSN(self, value):
        self.sn = value

    @property
    def getDetectEnter(self):
        return self.detectEnter

    @property
    def getDetectSpace(self):
        return self.detectSpace

    @property
    def getSampleCode(self):
        return self.sampleCode

    def setSampleCode(self, value):
        self.sampleCode = value

    @property
    def getJobnumber(self):
        return self.jobnumber

    @getJobnumber.setter
    def setJobnumber(self, value):
        self.jobnumber = value

    @property
    def getKeyboards(self):
        return self.keyboards

    @property
    def getMouse(self):
        return self.mouse

    @property
    def getBatteryState(self):
        return self.batteryState

    def toJson(self):
        '''
        json序列化
        :return:json字符串
        '''
        return json.dumps(self.__dict__)

    def toObj(self, value):
        '''
        转化成beanret对象
        :param value:json字符串
        :return:BeanRet对象
        '''
        self.__dict__ = json.loads(value)
        return self


if __name__ == '__main__':
    beanret = CacheData('123456798')
    beanret.setSN('8888')
    beanret.detectMouseKeys.append(1)
    print(beanret.getSN)
    print(beanret.getKeyboards.__len__())
    # for keycode in beanret.getKeyboards:
    #     beanret.detectKeys.append(keycode)

    del beanret.keyboards[:]
    print(beanret.toJson())
    jsonStr = json.dumps(beanret.__dict__)
    print(jsonStr)
    beanretJson = json.loads(jsonStr)
    beanret2 = CacheData()
    beanret2.toObj(jsonStr)
    print(beanret2.getSN)
